package com.suitsoft.bdms.service.system;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.suitsoft.bdms.dao.account.AuthorityDao;
import com.suitsoft.bdms.dao.account.TreeInfoDao;
import com.suitsoft.bdms.entity.account.Authority;
import com.suitsoft.bdms.entity.system.STreeInfo;
/**
 * 目录树管理 逻辑类.
 * @author Genius
 *
 */
@Service
@Transactional
public class TreeInfoManager {
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private TreeInfoDao treeInfoDao;
	@Autowired
	private AuthorityDao authorityDao;
	
	
	/**
	 * 查询所有目录信息.
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<Map<String,Object>> findAllTreeInfo(){
		return treeInfoDao.findAllTreeInfo();
	}
	
	/**
	 * 查询所有目录.
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<STreeInfo> findAllDir(){
		return treeInfoDao.findAllDir();
	}
	
	/**
	 * 重写save。
	 * @param entity
	 */
	public void save(Object entity){
		treeInfoDao.save1(entity);
	}
	
	/**
	 * 获取实体.
	 * @param id
	 * @return
	 */
	@Transactional(readOnly = true)
	public STreeInfo getEntiry(long id){
		return treeInfoDao.get(id);
	}
	
	/**
	 * 获取新的权限组ID.
	 * @return
	 */
	@Transactional(readOnly = true)
	public long getNewGoupId(){
		return treeInfoDao.getNewGroupId();
	}
	/**
	 * 获取实体.
	 * @param id
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<Authority> getAuthorityEntiry(long id){
		return authorityDao.find("from Authority a where a.groupId=?", id);
	}
	
	/**
	 * 删除实体. 
	 * @param entiry
	 */
	public void delete(Object entiry){
		treeInfoDao.delete1(entiry);
	}
	
	/**
	 * 删除权限表和资源表的中间.不删除会因为，找不到资源表而，报错.
	 * @param id 资源表ID
	 */
	public void deleteRole_Authority(long id) throws Exception{
		treeInfoDao.deleteRole_Authority(id);
	}
	
	/**
	 * 查询所有父级目录.
	 * @param id
	 * @return
	 */
	public List<STreeInfo> getSTreeInfosByParentId(long id){
		return treeInfoDao.find("from STreeInfo s where s.parentid=?", id);
	}
	
	/**
	 * 更新.
	 * @param entiry
	 */
	public void update(Object entiry){
		treeInfoDao.merge(entiry);
	}
	
	public void flush(){
		treeInfoDao.flush();
	}
	
}
